<?php

    class Usuario {
        private $id            = null;
        private $contrasena    = null;
        private $nombre        = null;
        private $apellido      = null;
        private $fecha_alta    = null;
        private $ultima_sesion = null;
        private $session_id    = null;

        function __construct() {}

        function get_id             () { return $this->id;            }
        function get_contrasena     () { return $this->contrasena;    }
        function get_nombre         () { return $this->nombre;        }
        function get_apellido       () { return $this->apellido;      }
        function get_fecha_alta     () { return $this->fecha_alta;    }
        function get_ultima_sesion  () { return $this->ultima_sesion; }
        function get_session_id     () { return $this->session_id;    }

        function set_id             ( $value ) { $this->id            = $value; }
        function set_contrasena     ( $value ) { $this->contrasena    = $value; }
        function set_nombre         ( $value ) { $this->nombre        = $value; }
        function set_apellido       ( $value ) { $this->apellido      = $value; }
        function set_fecha_alta     ( $value ) { $this->fecha_alta    = $value; }
        function set_ultima_sesion  ( $value ) { $this->ultima_sesion = $value; }
        function set_session_id     ( $value ) { $this->session_id    = $value; }

        static function GetByID( $id ) {
            $dbh = DBH::GetInstance()->get_dbh();

            $sth = $dbh->prepare('SELECT id, contrasena, nombre, apellido, fecha_alta, ultima_sesion FROM usuarios WHERE id = :id');
            $sth->bindParam( ':id', $id, PDO::PARAM_STR );
            $sth->execute();
            $result = $sth->fetch( PDO::FETCH_ASSOC );

            if( ! $result ) return false;

            $instance = new Usuario();
            $instance->set_id( $result['id'] );
            $instance->set_contrasena( $result['contrasena'] );
            $instance->set_nombre( $result['nombre'] );
            $instance->set_apellido( $result['apellido'] );
            $instance->set_fecha_alta( new DateTime($result['fecha_alta']) );
            $instance->set_ultima_sesion( new DateTime($result['ultima_sesion']) );

            return $instance;
        }

        function store_session( $user_id, $session_id ) {
            $dbh = DBH::GetInstance()->get_dbh();

            $sth = $dbh->prepare('UPDATE usuarios SET session_id = :session_id, ultima_sesion = NOW() WHERE id = :id');
            $sth->bindParam( ':id',         $user_id,    PDO::PARAM_STR );
            $sth->bindParam( ':session_id', $session_id, PDO::PARAM_STR );

            return $sth->execute();
        }

        function clear_session( $user_id ) {
            $dbh = DBH::GetInstance()->get_dbh();

            $sth = $dbh->prepare('UPDATE usuarios SET session_id = NULL WHERE id = :id');
            $sth->bindParam( ':id', $user_id, PDO::PARAM_STR );

            return $sth->execute();
        }

        function validate_user_status( $user_id, $session_id ) {
            $dbh = DBH::GetInstance()->get_dbh();

            $sth = $dbh->prepare('SELECT session_id FROM usuarios WHERE id = :id');
            $sth->bindParam( ':id', $user_id, PDO::PARAM_STR );
            $sth->execute();
            $result = $sth->fetch( PDO::FETCH_ASSOC );

            if( ! $result ) return false;

            if( $result['session_id'] != $session_id ) return false;
            else                                       return true;
        }
    }

?>